package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Date;
import java.util.UUID;


/**
 * Created by Administrator on 2017/11/27.
 */
public class SQLTemplate {
    private String sql;
    public SQLTemplate(){}
    public SQLTemplate(String sql){
        this.sql = sql;
    }
    public static void addSQL(String content,String status,String tablename) throws SQLException {

        String sql = "insert into "+tablename+" (id,content,time,timestamp,status) values(?,?,?,?,?)";
        PreparedStatement preparedStatement = null;
        Connection conn = null;
        try {
            conn = new DBconnector().connect();
            System.out.println(conn.getCatalog());
            if (conn.isClosed()){
                System.err.println("Error-connection已释放，不继续使用");
                System.err.println("执行失败");
                return;
            }
            preparedStatement = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("Error-addSQL");
        }
        String timestr = new Date().toString();
        Long timestamp = new Date().getTime();
        UUID uuid = UUID.randomUUID();
        preparedStatement.setString(1,uuid.toString()); // id
        preparedStatement.setString(2,content); // content
        preparedStatement.setString(3,timestr); // time
        preparedStatement.setString(4, String.valueOf(timestamp)); // timestamp
        preparedStatement.setString(5, status); // timestamp

        if(preparedStatement.execute()){//执行操作
            System.out.println("执行成功");
        }else{
            System.out.println("执行成功");
        }
        DBconnector.close(preparedStatement,conn);
        System.err.println("mysql 连接释放");
    }

    public void deleteSQL(){}
    public void updateSQL(){}
    public void selectSQL(){}
}
